package com.jvnsrp.mirror;

import com.jcraft.jsch.JSchException;

import java.io.FileReader;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Properties;

/**
 * @author Jevon
 * @version V1.0
 * @date 2021/6/20 20:57
 **/
public class MainFile {
    public static void main(String[] args) {
        /**
         * 1. 读取配置文件
         * 2. 计算服务器目录md5值
         * 3. 获取服务器md5文件到本地
         * 4. 遍历本地目录获取map
         * 5. 遍历md5文件校对map
         * 6. 生成删除文件列表
         * 7. 生成复制文件列表
         * 8. sftp发送文件
         * 9. sftp删除文件
         */
        try {
            Properties properties = getConfig("target\\classes\\config.properties");

            String host = properties.getProperty("host");
            int port = 22;
            String username = properties.getProperty("username");
            String password = properties.getProperty("password");
            int timeout = 600;

            SftpUtils sftp = new SftpUtils(host, port, username, password, timeout);
            String source = properties.getProperty("source");
            String target = properties.getProperty("target");
            String md5file = properties.getProperty("md5");
            String f = "index.html";
//            sftp.cd(target);
//            sftp.put(source + f,  target+"abc/"+f);
//            sftp.rmdir(target+"abc");
            sftp.md5sum(target, md5file);
            sftp.get(md5file, source + "/test.md5");
            sftp.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSchException e) {
            System.out.println("sftp error");
            e.printStackTrace();
        }

    }

    /**
     * 读取配置文件
     *
     * @return
     * @throws IOException
     */
    public static Properties getConfig(String path) throws IOException {
        Properties properties = new Properties();
        System.out.println(Path.of(path).toAbsolutePath());
        if (Files.exists(Path.of(path))) {
            properties.load(new FileReader(path));
        }
        return properties;
    }

    public static void test3() {
        String path = "D:\\MyGitee\\puzzle\\dist";
        Path p = Path.of(path);

        long l1 = System.currentTimeMillis();
        Map map = MD5Utils.getMd5map(p.toFile());
        long l2 = System.currentTimeMillis();
        System.out.println(l2 - l1);
        String key = (String) map.keySet().iterator().next();
        System.out.println(key);
        System.out.println(key.substring(path.length()));
    }

    public static void test1() {
        String hello_world = MD5Utils.md5sums("hello world");
        System.out.println(hello_world);
        //        Path p = Path.of("D:\\MyTemp\\test.txt");
        Path p = Path.of("D:\\01-软件\\qt-opensource-windows-x86-5.12.9.exe");
        long l1 = System.currentTimeMillis();

        String s = MD5Utils.md5sums(p.toFile());
//        String h = "f5bb0c8de146c67b44babbf4e6584cc0";
        long l2 = System.currentTimeMillis();
        String h = "b9257df07dac3cbac910759c3b962aef";
        System.out.println(s);
        System.out.println(s.equals(h));
        System.out.println(l2 - l1);
    }

    public static void test2() {
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage(); //椎内存使用情况
        long totalMemorySize = memoryUsage.getInit(); //初始的总内存
        long maxMemorySize = memoryUsage.getMax(); //最大可用内存
        long usedMemorySize = memoryUsage.getUsed(); //已使用的内存
        System.out.println("初始的总内存:" + totalMemorySize);
        System.out.println("最大可用内存:" + maxMemorySize);
        System.out.println("已使用的内存:" + usedMemorySize);
    }


}
